package com.jair.controller;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.jair.domain.Scores;
import com.jair.service.impl.ScoresServiceImpl;


@Controller
@RequestMapping(value = "/test/")
public class TestPathController1 {
	@Autowired
	private ScoresServiceImpl ScoresService;
	
	@RequestMapping(value = "importExcel" ,method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> importExcel(@RequestParam MultipartFile file){
		Map<String, Object> resultMap = new HashMap<String,Object>();
		try {
			InputStream is = file.getInputStream();
			Workbook wb = null;
			 if (file.getOriginalFilename().contains("xlsx")) {
			        wb = new XSSFWorkbook(is);
			    } else {
			        wb = new HSSFWorkbook(is);
			    }
			List<Scores> scoresList = this.ScoresService.parserExcelToScore(wb);
			System.out.println(scoresList);
		}catch (Exception e) {
			// TODO: handle exception
		}
		return resultMap;
	}
}
